var addcount=1;
var gametime=1;
var canbegin=false;
var Player={
    lingShiCount:[0,0,0,100],
    bagA:new Array(),
    bagB:new Array(),
    gametime:[0,0,0,0,0],

    action:{
        findMed:0,
        findMedN:0
    },
    lastTime:0,
    version:3
}
var showAddMap=new Map();
var showCovMap=new Map();

var GameSet={
    iTime:40
}
var canSave=false;

var bTime=0;
var eTime=0;
var lastTime=0;
var iTime=GameSet.iTime;

var testmsg="";
var findMedFuc=null;
var findMedFucN=null;
  
setTimeout(function(){
    check();
    loading();
    canbegin=true;
},0)

try{
    setInterval(function(){
        
        try{
        if(canbegin){
        getBeginTime();
        gameAction();
        gameTimeAdd();
        gameShow();
        getEndTime();
        }
        }catch(e){
            ShowtestMsg("出现了一些问题，游戏无法进行！<br>请更换浏览器试试"+e.message,true);
            iTime=9999999;
        }
    },iTime);
}catch(e){
    ShowtestMsg("出现了一些问题，游戏无法进行！<br>请更换浏览器试试"+e.message,true);
    iTime=9999999;
}


function gameAction(){
    ShowtestMsg(gametime,true)
    if(gametime%1000==0)
        saveGame();
    var i=new Item();
    if(gametime%25==0){
        Player.gametime[0]++;
        if(Player.gametime[0]>=60){
            Player.gametime[0]%=60;
            Player.gametime[1]++;
        }
        if(Player.gametime[1]>=60){
            Player.gametime[1]%=60;
            Player.gametime[2]++;
        }
        if(Player.gametime[2]>=24){
            Player.gametime[2]%=24;
            Player.gametime[3]++;
        }
        if(Player.gametime[3]>=365){
            Player.gametime[3]%=365;
            Player.gametime[4]++;
        }

        if(findMedFuc!=null)
            findMedFuc(findMedFucN);
    }

    
    
}

function gameTimeAdd(){
    gametime++;
}

function gameShow(){
    document.getElementById("gametime").innerHTML=Player.gametime.toString();
    document.getElementById("lingshiCount").innerText=Player.lingShiCount.toString();

    showAddMap.forEach(function(value, key) {
        var elm=document.getElementById(key);
        if(elm){
            if(elm.innerHTML.length>=1000)
                elm.innerHTML=value+"<br>";
            else
                elm.innerHTML+=value+"<br>";
        }
    })
    showAddMap.clear();
    showCovMap.forEach(function(value, key) {
        var elm=document.getElementById(key);
        if(elm){
            elm.innerHTML=value;
        }
    })
    showCovMap.clear();

}

function ShowtestMsg(msg,flag=false){
    if(flag){
        document.getElementById("sysMsg").innerHTML=msg;
        
    }
    else{
        if(document.getElementById("sysMsg").innerHTML.length>=500)
            document.getElementById("sysMsg").innerHTML=msg;
        else
            document.getElementById("sysMsg").innerHTML+=msg;
    }
}

function showdata(name){
    var elm=document.getElementById("dataShow").children;
    for(var i=0;i<elm.length;i++){
        elm[i].style="display:none";
    }
    document.getElementById(name).style="display:block"
}

function showdel(){
}

function showBag(){
    var s="";
    Player.bagA.forEach(function(i){
        s+="<div class='d-flex'>"
        s+="<div class='bagItemName'>"+i.name+"</div>";
        s+="<div class='bagItemCount'>【&nbsp"+i.count+"&nbsp】</div><br>";
        s+="</div>"
    })

    Player.bagB.forEach(function(i){
        s+="<div class='d-flex'>"
        s+="<div class='bagItemName'>"+i.name+"</div>";
        s+="<div class='bagItemCount'>【&nbsp"+i.count+"&nbsp】</div><br>";
        s+="</div>"
    })
    document.getElementById("bag").innerHTML=s;
}

function getMchClick(level){
    findMedFucN=level;
    findMedFuc=MapFindMed;
    Player.action.findMed=1;
    Player.action.findMedN=level;

    showAddMap.set("findMedMsg","你开始了寻药！")
}


function saveGame(){
    if(canSave){
        var json=JSON.stringify(Player)
        var str=Base64.encode(json);
        localStorage.setItem("gamesave", str);
    }
}

function loading(){
    if(canSave){
        var json= localStorage.getItem("gamesave");
        if(json!=null&&json.length>0){
            var str=Base64.decode(json)
            var p=JSON.parse(str);
            if(p.version==undefined){
                p.version=1;
                p.gametime=[0,0,0,0,0];
            }
            switch(p.version){
                case 1:    
                p.action={
                    findMed:0,
                    findMedN:0
                };
                var d=new Date();
                p.lastTime=d.getTime();
                p.version=2;
                case 2:
                    localStorage.removeItem("gamesave");
                    p=Player;
                    break;
            }

            Player=p;
            switch(Player.action.findMed){
                case 1:
                    findMedFuc=MapFindMed;
                    findMedFucN=Player.action.findMedN;
            }
            getLeaveTimeAction();
        }
    }

}

function check(){
    if(!(typeof(Storage)!=="undefined"))
    {
        document.getElementById("sysMsg").innerText="对不起，您的浏览器不支持 web 存储。";
    }else{
        canSave=true;
    }
}

function playerBagAdd(item){
    var tmp=new Item().copy(item);
    if(tmp.id>5000){
        var havein=false;
        for(var i=0;i<Player.bagB.length;i++){
            if(Player.bagB[i].ItemId==tmp.ItemId){
                Player.bagB[i].count+=tmp.count;
                havein=true;
                break;
            }
        }
        if(!havein){
            Player.bagB.push(tmp);
        }
    }
    else{
        var havein=false;
        for(var i=0;i<Player.bagA.length;i++){
            if(Player.bagA[i].ItemId==tmp.ItemId){
                Player.bagA[i].count+=tmp.count;
                havein=true;
                break;
            }
        }
        if(!havein){
            Player.bagA.push(tmp);
        }
    }

    showBag();
}

function getBeginTime(){
    bTime=new Date().getMilliseconds();
    Player.lastTime=new Date().getTime();
}

function getEndTime(){
    eTime=new Date().getMilliseconds();
    var tmp=GameSet.iTime-(eTime-bTime);
    iTime=(tmp>0)?tmp:0;
    getLeaveTimeAction();
    
}

function getLeaveTimeAction(){
    var d=new Date();
    var t=d.getTime()-Player.lastTime;
    if(t>10000&&t<999999999){
        do{
            gameAction();
            gameTimeAdd();
            gameShow();
            t-=40;

        }while(t>40);
        var d2=new Date();
        Player.lastTime=d2.getTime();
        saveGame();
    }

}

